package com.dr.dac.knowledgebase.ui.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.dr.dac.knowledgebase.R;
import com.dr.dac.knowledgebase.db.KClassDataBase;
import com.dr.dac.knowledgebase.db.KContentDataBase;
import com.dr.dac.knowledgebase.db.KFunctionDataBase;
import com.dr.dac.knowledgebase.ui.adapter.KClassAdapter;
import com.dr.dac.knowledgebase.ui.adapter.MyAdapter;
import com.dr.dac.knowledgebase.ui.adapter.RightAdapter;
import com.dr.dac.knowledgebase.ui.bean.KClass;
import com.dr.dac.knowledgebase.ui.bean.KContent;
import com.dr.dac.knowledgebase.ui.bean.KFunction;
import com.dr.dac.knowledgebase.ui.service.AppServer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button step1Btn;
    Button step2Btn;
    Button step3Btn;

    DrawerLayout drawerLayout;

    RecyclerView kclassRv;
    RecyclerView kfuncRv;
    RecyclerView rightRv;

    KClassDataBase kClassDataBase;
    KFunctionDataBase kFunctionDataBase;
    KContentDataBase kContentDataBase;


    MyAdapter adapter;
    KClassAdapter kClassAdapter;
    RightAdapter rightAdapter;
    List<KFunction> list = new ArrayList<>();
    List<KClass> kclass_list = new ArrayList<>();

    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        kClassDataBase = new KClassDataBase(this);
        kFunctionDataBase = new KFunctionDataBase(this);
        kContentDataBase = new KContentDataBase(this);


        step1Btn = (Button) findViewById(R.id.step1);
        step2Btn = (Button) findViewById(R.id.step2);
        step3Btn = (Button) findViewById(R.id.step3);
        step1Btn.setOnClickListener(this);
        step2Btn.setOnClickListener(this);
        step3Btn.setOnClickListener(this);

        kclassRv = (RecyclerView) findViewById(R.id.kclass_rv);
        kclassRv.setLayoutManager(new LinearLayoutManager(this));
        kClassAdapter = new KClassAdapter(this);
        kclassRv.setAdapter(kClassAdapter);


        kfuncRv = (RecyclerView) findViewById(R.id.kfunc_rv);
        kfuncRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyAdapter(this);
        kfuncRv.setAdapter(adapter);

        rightRv = (RecyclerView) findViewById(R.id.right_recyclerView);
        rightRv.setLayoutManager(new LinearLayoutManager(this));
        rightAdapter = new RightAdapter(this);
        rightRv.setAdapter(rightAdapter);
        drawerLayout = (DrawerLayout) findViewById(R.id.draw_layout);
        adapter.setOnItemClickListener(new MyAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

            }
        });

        //intent = new Intent(this,AppServer.class);
        //bindService(intent, this, BIND_AUTO_CREATE);
    }

    private void setSnackBar(final List<KContent> contents) {
        Snackbar.make(findViewById(R.id.root), "您有一条新推送", Snackbar.LENGTH_INDEFINITE).setAction("点击查看详情", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rightAdapter.setDatas(contents);
                rightAdapter.notifyDataSetChanged();
                drawerLayout.openDrawer(Gravity.RIGHT);
            }
        }).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.step1:

                break;
            case R.id.step2:

                break;
            case R.id.step3:
                break;
        }
    }

//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//
//            switch (msg.what){
//                case AppServer.STEP_1:
//                    kclass_list = (List<KClass>) msg.obj;
//                    kClassAdapter.setDatas(kclass_list);
//                    kClassAdapter.notifyDataSetChanged();
//                    break;
//                case AppServer.STEP_2:
//                    list = (List<KFunction>) msg.obj;
//                    adapter.setDatas(list);
//                    adapter.notifyDataSetChanged();
//                    break;
//                case AppServer.CONTENT_3:
//                    setSnackBar((List<KContent>) msg.obj);
//                    break;
//            }
//
//        }
//    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //unbindService(this);
    }
}
